<?php
class Balance{
	public $site_id = 0;
	//构造函数
	public function Balance($site_id = 0){
		if($site_id == 0){
			$site_id = defined('__ADMINSITEID__')?__ADMINSITEID__:__SITEID__;
		}
		
		$this->site_id = $site_id;
		
	
	}
	//创建设置项表单
	public function createform(){
		
		
	}
	
	//保存设置项
	public function saveform(){
		
	}
	
	//发起支付
	public function pay($orderid){
	    
	   
	    $Integral_db=D('Integral_log');
		$ordermes = M('Order')->where(array('id'=>$orderid, 'site_id' => $this->site_id))->find();
		
		
		$Setbody = $ordermes['ordernumber'].$ordermes['id'];
		$SetTotal_fee = (($ordermes['total']-$ordermes['shipping_fee'])*1.06)+$ordermes['shipping_fee'];
		$member = M('Member') -> where(array('id' => $ordermes['member_id'])) -> find();
		//余额是否足够
		/*$sale=M('Sale')->where(array('order_id'=>$orderid))->select();
		foreach($sale as $k => $v){
		$product=M('Product')->where(' id = '.$v['product_id'])->find();	
		$supplier=M('Supplier')->where(' id = '.$product['supplier_id'])->find();
			if($supplier['type']==2){
			$off_total+=$product['total'];	
			}
			if($off_total>$member['offline']){
			$html = '<script>alert(\'支付失败！积分不足！\');</script>';
			return $html;
			}
			
		}*/
		
		
		$member['money']=$member['offline'];
		
		$data['order_id'] = $orderid;
		if($member['money'] >= $SetTotal_fee){
			$ret = D('Order') -> upstate($ordermes['ordernumber']);
			
			if($ret){
				$data['total'] = ($member['offline'])+($SetTotal_fee*-1);
			   // $data['total'] = ($member['online']+$member['offline'])+($SetTotal_fee*-1);
			    $data['member_id'] = $ordermes['member_id'];
			    $data['remark'] = '支付订单'.$ordermes['ordernumber']; 	
				$data['time'] = time();
			/*if($member['offline']>$SetTotal_fee){
				$data['user_id'] = 0;
				$data['offline'] = ($SetTotal_fee)*-1;
				$data['online'] = 0;
				//$save['offline']=(int)($member['offline']-$SetTotal_fee);
				D('Integral_log') -> add($data);
				D('Member') -> where(array('id' => $ordermes['member_id'])) -> save($save);
				$html = '<script>setTimeout(function(){$("#payPro").html("支付成功");
				$("#payend").show();},500);</script>';
				return $html;
				//exit();
				
			}			
				//$SetTotal_fee=$SetTotal_fee-$meber['offline'];
				//$data['total'] = ($SetTotal_fee)*-1;
				//$data['dateline'] = time();
				
				$data['offline'] =($member['offline'])*-1;
				$data['online']=($data['total']+$member['offline']);
				*/	
				
				//exit(dump($data));
				$data['offline'] = ($SetTotal_fee*-1);
				D('Integral_log') -> add($data);
				/*$online['online']=$member['online']-(($ordermes['total']-$member['offline']));
				$offline['offline']=0;
				
				D('Member') -> where(array('id' => $ordermes['member_id'])) -> save($online);
					D('Member') -> where(array('id' => $ordermes['member_id'])) -> save($offline);*/
					
				$res=D('Member') -> where(array('id' => $ordermes['member_id'])) -> save(array('offline'=>($data['total'])));
				if($res){
				M('Order')->where(' id = '.$orderid)->save(array('integral_pay'=>$SetTotal_fee));
				}
				$html = '<script>setTimeout(function(){$("#payPro").html("支付成功");
				$("#payend").show();},500);</script>';
			}
		}else{
			$html = '<script>alert(\'支付失败！积分不足！\');</script>';
		}
		 
		return $html;
	}
	public function callback(){
	}
	
}

	

